package com.ihd.ihardware.view.tzc.me.view;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.google.gson.Gson;
import com.ihd.ihardware.R;
import com.ihd.ihardware.app.MyApplication;
import com.ihd.ihardware.common.AppConstans;
import com.ihd.ihardware.common.widget.SwipeItemLayout;
import com.ihd.ihardware.databinding.ActivityMemberBinding;
import com.ihd.ihardware.pojo.MembersRes;
import com.ihd.ihardware.pojo.UserInfoRes;
import com.ihd.ihardware.view.tzc.me.viewmodel.MemberViewModel;

import java.util.ArrayList;
import java.util.List;

import cn.wowjoy.commonlibrary.base.BaseActivity;
import cn.wowjoy.commonlibrary.base.BaseConsumer;
import cn.wowjoy.commonlibrary.utils.SPUtils;
import cn.wowjoy.commonlibrary.utils.ToastUtils;

public class MemberActivity extends BaseActivity<ActivityMemberBinding, MemberViewModel> {

    private UserInfoRes.DataBean ud;

    @Override
    protected void init(Bundle savedInstanceState) {
        mImmersionBar.statusBarDarkFont(true).init();
        binding.mtitlebar.setTitle("家庭成员");
        binding.mtitlebar.setLeftBack(this);

        ud = new Gson().fromJson(SPUtils.getString(AppConstans.USER_INFO, ""), UserInfoRes.DataBean.class);
        binding.addMemberTV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MemberActivity.this, AddMemberActivity.class);
                startActivity(i);
            }
        });

        setRx(AppConstans.DELMEMBER, new BaseConsumer<MembersRes>() {
            @Override
            protected void onStart() {
            }

            @Override
            protected void onSuccess(MembersRes mLiveData) {
                viewModel.setMembers(mLiveData.getData());
                MyApplication.membersData.clear();
                MembersRes.DataBean md = new MembersRes.DataBean();
                md.setAvatar(ud.getAvatar());
                md.setBirthday(ud.getBirthDay());
                md.setHeight(ud.getHeight());
                md.setMemberId("");
                md.setWeight(ud.getWeight());
                md.setUserId(ud.getUserId());
                md.setSex(ud.getSex());
                md.setRelation(0);
                MyApplication.membersData.add(md);
                if (mLiveData.getData() != null && mLiveData.getData().size() > 0) {
                    MyApplication.membersData.addAll(mLiveData.getData());
                    binding.emptyLL.setVisibility(View.GONE);
                } else {
                    binding.emptyLL.setVisibility(View.VISIBLE);
                }
                MyApplication.membersData.add(new MembersRes.DataBean());
            }

            @Override
            protected void onError(Throwable error) {
                ToastUtils.showShort(MemberActivity.this, error.getMessage());
            }
        });

        binding.membersRV.addOnItemTouchListener(new SwipeItemLayout.OnSwipeItemTouchListener(this));
        binding.membersRV.setAdapter(viewModel.mMemberAdapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        List<MembersRes.DataBean> md = new ArrayList<>();
        md.addAll(MyApplication.membersData);
        md.remove(md.size() - 1);
        md.remove(0);
        viewModel.setMembers(md);
        if (md.size() > 0)
            binding.emptyLL.setVisibility(View.GONE);
        else
            binding.emptyLL.setVisibility(View.VISIBLE);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_member;
    }

    @Override
    protected Class getViewModelClass() {
        return MemberViewModel.class;
    }

}
